﻿using FastCtr;
using M6R小圆柱注液机.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FastData;

namespace M6R小圆柱注液机.Pages
{
    public partial class SettingPage : Form
    {
        public SettingPage()
        {
            InitializeComponent();
        }

        private void SettingPage_Load(object sender, EventArgs e)
        {
            tbWeightValue.Text = SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "CheckOut_Para", "WeightValue");
            tbCheckError.Text = SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "CheckOut_Para", "CheckError");
            tbMechineNumber.Text = SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "AssetsEncoding");
            tbProgramVersion.Text = SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "ProgramVersion");
            tbCodeTop.Text = SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "ElectrolyteBatchFrontSerialNumber");
            tbClosedLoopTRAS.Text = GlobalValues.CommonVariable.closedLoopTRAS.ToString();

            if (GlobalValues.StatusVariable.isUseRadomCode) { cbRadomCode.Checked = true; }
            else { cbRadomCode.Checked = false; }

            TimerLimit();
        }

        private void TimerLimit()
        {
            Timer tLimit = new Timer();
            tLimit.Interval = 1000;
            tLimit.Tick += TLimit_Tick;
            tLimit.Start();
        }



        /// <summary>
        /// 电子秤1配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Controls.Add(Data.GlobalValues.Fore_And1);
            form.ShowDialog();
        }
        /// <summary>
        /// 电子秤2配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Text = "前电子称2";
            form.AutoSize = true;
            form.Controls.Add(Data.GlobalValues.Fore_And2);
            form.ShowDialog();
        }
        /// <summary>
        /// 电子秤3配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Text = "后电子称1";
            form.AutoSize = true;
            form.Controls.Add(Data.GlobalValues.Behind_And1);
            form.ShowDialog();
        }
        /// <summary>
        /// 电子秤4配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Text = "后电子称2";
            form.AutoSize = true;
            form.Controls.Add(Data.GlobalValues.Behind_And2);
            form.ShowDialog();
        }
        /// <summary>
        /// 电子秤点检
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWeightCheck_Click(object sender, EventArgs e)
        {
            GlobalValues.pointCheck.ShowDialog();
        }

        /// <summary>
        /// MFG - - 基本信息验证的串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMFGSetting_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Text = "MFG基本信息验证串口设置";
            form.AutoSize = true;
            form.Controls.Add(Data.GlobalValues.serialPort5);
            form.ShowDialog();
        }
        /// <summary>
        /// MFG - - 电解液验证的串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMFG_ElectrolyteSetting_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Text = "MFG电解液验证串口设置";
            form.AutoSize = true;
            form.Controls.Add(Data.GlobalValues.serialPort6);
            form.ShowDialog();
        }
        /// <summary>
        /// MFG - - 更换电解液后验证的串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMFG_ESNSetting_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Text = "MFG更换电解液后验证的串口的设置";
            form.AutoSize = true;
            form.Controls.Add(Data.GlobalValues.serialPort7);
            form.ShowDialog();
        }

        /// <summary>
        /// 扫码枪1配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScanner_1Setting_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Controls.Add(Data.GlobalValues.newScanner_1);

            form.Text = "扫码枪1";

            form.ShowDialog();
        }
        /// <summary>
        /// 扫码枪2配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScanner_2Setting_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Controls.Add(Data.GlobalValues.newScanner_2);
            form.Text = "扫码枪2";
            form.ShowDialog();
        }
        /// <summary>
        /// 扫码枪3配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScanner_3Setting_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Controls.Add(Data.GlobalValues.newScanner_3);
            form.Text = "扫码枪3";
            form.ShowDialog();
        }

        /// <summary>
        /// PLC配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPLCSetting_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Controls.Add(Data.GlobalValues.njOmronPLC);
            form.Text = "PLC设置";
            form.Show();
        }


        /// <summary>
        /// 更改砝码重量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeWeightValue_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认更改砝码重量？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                if (tbWeightValue.Text == "")
                {
                    MessageBox.Show("输入的砝码重量不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveStatic.SaveIni(GlobalValues.PathVariable.fileCommonPara, "CheckOut_Para", "WeightValue", tbWeightValue.Text);
                    if (eventChangingInfomation_SettingPage != null)
                    {
                        eventChangingInfomation_SettingPage(tbWeightValue.Text, 0);
                    }
                    MessageBox.Show("修改成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        /// <summary>
        /// 更改点检误差
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeCheckError_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认更改点检误差值？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                if (tbCheckError.Text == "")
                {
                    MessageBox.Show("输入的点检误差值不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveStatic.SaveIni(GlobalValues.PathVariable.fileCommonPara, "CheckOut_Para", "CheckError", tbCheckError.Text);
                    if (eventChangingInfomation_SettingPage != null)
                    {
                        eventChangingInfomation_SettingPage(tbCheckError.Text, 1);
                    }
                    MessageBox.Show("修改成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        /// <summary>
        /// 更改机台号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeMechineNumber_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认更改设备号？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                if (tbMechineNumber.Text == "")
                {
                    MessageBox.Show("输入的设备号不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveStatic.SaveIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "AssetsEncoding", tbMechineNumber.Text);
                    if (eventChangingInfomation_SettingPage != null)
                    {
                        eventChangingInfomation_SettingPage(tbMechineNumber.Text, 2);
                    }
                    MessageBox.Show("修改成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        /// <summary>
        /// 更改程序版本号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeProgramVersion_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认更改程序版本号？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                if (tbProgramVersion.Text == "")
                {
                    MessageBox.Show("输入的程序版本号不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveStatic.SaveIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "ProgramVersion", tbProgramVersion.Text);
                    if (eventChangingInfomation_SettingPage != null)
                    {
                        eventChangingInfomation_SettingPage(tbProgramVersion.Text, 3);
                    }
                    MessageBox.Show("修改成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        /// <summary>
        /// 更换电解液批次号前几位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeCodeTop_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认更改电解液批次号前几位？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                if (tbCodeTop.Text == "")
                {
                    MessageBox.Show("输入的电解液批次号前几位不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveStatic.SaveIni(GlobalValues.PathVariable.fileCommonPara, "Machine_Para", "ElectrolyteBatchFrontSerialNumber", tbCodeTop.Text);
                    if (eventChangingInfomation_SettingPage != null)
                    {
                        eventChangingInfomation_SettingPage(tbCodeTop.Text, 4);
                    }
                    MessageBox.Show("修改成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        /// <summary>
        /// 更改注液量闭环周期
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeClosedLoopTRAS_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认更改注液量闭环周期？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                if (tbClosedLoopTRAS.Text == "")
                {
                    MessageBox.Show("输入的注液量闭环周期不能为空！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveStatic.SaveIni(GlobalValues.PathVariable.fileCommonPara, "CheckOut_Para", "ClosedLoopTRAS", tbClosedLoopTRAS.Text);
                    if (eventChangingInfomation_SettingPage != null)
                    {
                        eventChangingInfomation_SettingPage(tbClosedLoopTRAS.Text, 5);
                    }
                    MessageBox.Show("修改成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        /// <summary>
        /// 电芯条码是否使用随机码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbRadomCode_CheckedChanged(object sender, EventArgs e)
        {
            if (cbRadomCode.CheckState == CheckState.Checked)
            {
                //表示当前，单选框处于选中状态，条码启用随机码
                GlobalValues.StatusVariable.isUseRadomCode = true;
                GlobalValues.log_Helper.DispProcess("启用随机条码", "Info", true);
            }
            else
            {
                GlobalValues.StatusVariable.isUseRadomCode = false;
                GlobalValues.log_Helper.DispProcess("禁用随机条码", "Info", true);
            }
        }



        private void button5_Click(object sender, EventArgs e)
        {
            调试 a = new 调试();
            a.ShowDialog();
        }

        private void TLimit_Tick(object sender, EventArgs e)
        {
            if (GlobalValues.StatusVariable.isAdminLoginSuccess)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = true;
                btnScanner_1Setting.Enabled = false;
                btnScanner_2Setting.Enabled = false;
                btnScanner_3Setting.Enabled = false;
                btnMFGInfomationSetting.Enabled = false;
                btnMFG_ElectrolyteSetting.Enabled = false;
                btnMFG_ESNSetting.Enabled = false;
                btnPLCSetting.Enabled = false;
                btnChangeMechineNumber.Enabled = true;
                btnChangeProgramVersion.Enabled = true;
                btnChangeCodeTop.Enabled = true;
                btnChangeClosedLoopTRAS.Enabled = true;
                btnChangeWeightValue.Enabled = true;
                btnChangeCheckError.Enabled = true;
            }
            else if (GlobalValues.StatusVariable.isEngineerLoginSuccess)
            {
                button1.Enabled = true;
                button2.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                btnScanner_1Setting.Enabled = true;
                btnScanner_2Setting.Enabled = true;
                btnScanner_3Setting.Enabled = true;
                btnMFGInfomationSetting.Enabled = true;
                btnMFG_ElectrolyteSetting.Enabled = true;
                btnMFG_ESNSetting.Enabled = true;
                btnPLCSetting.Enabled = true;
                btnChangeMechineNumber.Enabled = true;
                btnChangeProgramVersion.Enabled = true;
                btnChangeCodeTop.Enabled = true;
                btnChangeClosedLoopTRAS.Enabled = true;
                btnChangeWeightValue.Enabled = true;
                btnChangeCheckError.Enabled = true;
            }
            else
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
                btnScanner_1Setting.Enabled = false;
                btnScanner_2Setting.Enabled = false;
                btnScanner_3Setting.Enabled = false;
                btnMFGInfomationSetting.Enabled = false;
                btnMFG_ElectrolyteSetting.Enabled = false;
                btnMFG_ESNSetting.Enabled = false;
                btnPLCSetting.Enabled = false;
                btnChangeMechineNumber.Enabled = false;
                btnChangeProgramVersion.Enabled = false;
                btnChangeCodeTop.Enabled = false;
                btnChangeClosedLoopTRAS.Enabled = false;
                btnChangeWeightValue.Enabled = false;
                btnChangeCheckError.Enabled = false;
            }
        }





        /// <summary>
        /// 用于传递设置界面修改的值 到主界面 的委托
        /// </summary>
        /// <param name="msg">值</param>
        /// <param name="num">序号（不同的序号，对应不同的控件发生的事件）</param>
        public delegate void delegateChangingInfomation_SettingPage(string msg, int num);
        /// <summary>
        /// 用于传递设置界面修改的值 到主界面
        /// </summary>
        public event delegateChangingInfomation_SettingPage eventChangingInfomation_SettingPage;


    }
}
